// import { turn } from "core-js/fn/array"
export default {
  addToCart (state, payload) {
    // 添加购物车判断是否存在存在就count++不存在就push
    const isExist = state.cart.some(shop => shop.id === payload.id)
    if (isExist) {
      state.cart = state.cart.map(shop => {
        if (shop.id === payload.id) shop.count++
        return shop
      })
    } else {
      state.cart.push({
        ...payload,
        count: 1,
        check: true
      })
    }
  },
  toggleCheck (state, payload) {
    state.cart.check = state.cart.map(shop => {
      if (shop.id === payload) {
        shop.check = !shop.check
      }
      return shop
    })
  },
  changeCountById (state, { id, num }) {
    state.cart = state.cart.map(shop => {
      if (shop.id === id) shop.count = num
      return shop
    })
  },
  // 删除购物车
  delShop (state, id) {
    state.cart = state.cart.filter(shop => {
      return shop.id !== id.id
    })
  },
  // 全选按钮
  toggleAllCheck (state, { isAllCheck }) {
    // console.log(isAllCheck)
    state.cart = state.cart.map(shop => {
      shop.check = !isAllCheck
      // console.log(shop.check)
      return shop
    })
  }
}
